| Conditions | 1 |
| Paths | 1 |
| Total Lines | 122 |
| Code Lines | 81 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { processStatsFromVisits } from '../../../src/visits/services/VisitsParser'; |
||
| 3 | describe('VisitsParser', () => { |
||
| 4 | const visits = [ |
||
| 5 | { |
||
| 6 | userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0', |
||
| 7 | referer: 'https://google.com', |
||
| 8 | visitLocation: { |
||
| 9 | countryName: 'Spain', |
||
| 10 | cityName: 'Zaragoza', |
||
| 11 | latitude: '123.45', |
||
| 12 | longitude: '-543.21', |
||
| 13 | }, |
||
| 14 | }, |
||
| 15 | { |
||
| 16 | userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0', |
||
| 17 | referer: 'https://google.com', |
||
| 18 | visitLocation: { |
||
| 19 | countryName: 'United States', |
||
| 20 | cityName: 'New York', |
||
| 21 | latitude: '1029', |
||
| 22 | longitude: '6758', |
||
| 23 | }, |
||
| 24 | }, |
||
| 25 | { |
||
| 26 | userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', |
||
| 27 | visitLocation: { |
||
| 28 | countryName: 'Spain', |
||
| 29 | }, |
||
| 30 | }, |
||
| 31 | { |
||
| 32 | userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', |
||
| 33 | referer: 'https://m.facebook.com', |
||
| 34 | visitLocation: { |
||
| 35 | countryName: 'Spain', |
||
| 36 | cityName: 'Zaragoza', |
||
| 37 | latitude: '123.45', |
||
| 38 | longitude: '-543.21', |
||
| 39 | }, |
||
| 40 | }, |
||
| 41 | { |
||
| 42 | userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41', |
||
| 43 | }, |
||
| 44 | ]; |
||
| 45 | |||
| 46 | describe('processStatsFromVisits', () => { |
||
| 47 | let stats; |
||
| 48 | |||
| 49 | beforeAll(() => { |
||
| 50 | stats = processStatsFromVisits({ id: 'id', visits }); |
||
| 51 | }); |
||
| 52 | |||
| 53 | it('properly parses OS stats', () => { |
||
| 54 | const { os } = stats; |
||
| 55 | |||
| 56 | expect(os).toEqual({ |
||
| 57 | Linux: 3, |
||
| 58 | Windows: 1, |
||
| 59 | MacOS: 1, |
||
| 60 | }); |
||
| 61 | }); |
||
| 62 | |||
| 63 | it('properly parses browser stats', () => { |
||
| 64 | const { browsers } = stats; |
||
| 65 | |||
| 66 | expect(browsers).toEqual({ |
||
| 67 | Firefox: 2, |
||
| 68 | Chrome: 2, |
||
| 69 | Opera: 1, |
||
| 70 | }); |
||
| 71 | }); |
||
| 72 | |||
| 73 | it('properly parses referrer stats', () => { |
||
| 74 | const { referrers } = stats; |
||
| 75 | |||
| 76 | expect(referrers).toEqual({ |
||
| 77 | 'Unknown': 2, |
||
| 78 | 'google.com': 2, |
||
| 79 | 'm.facebook.com': 1, |
||
| 80 | }); |
||
| 81 | }); |
||
| 82 | |||
| 83 | it('properly parses countries stats', () => { |
||
| 84 | const { countries } = stats; |
||
| 85 | |||
| 86 | expect(countries).toEqual({ |
||
| 87 | 'Spain': 3, |
||
| 88 | 'United States': 1, |
||
| 89 | 'Unknown': 1, |
||
| 90 | }); |
||
| 91 | }); |
||
| 92 | |||
| 93 | it('properly parses cities stats', () => { |
||
| 94 | const { cities } = stats; |
||
| 95 | |||
| 96 | expect(cities).toEqual({ |
||
| 97 | 'Zaragoza': 2, |
||
| 98 | 'New York': 1, |
||
| 99 | 'Unknown': 2, |
||
| 100 | }); |
||
| 101 | }); |
||
| 102 | |||
| 103 | it('properly parses cities stats with lat and long', () => { |
||
| 104 | const { citiesForMap } = stats; |
||
| 105 | const zaragozaLat = 123.45; |
||
| 106 | const zaragozaLong = -543.21; |
||
| 107 | const newYorkLat = 1029; |
||
| 108 | const newYorkLong = 6758; |
||
| 109 | |||
| 110 | expect(citiesForMap).toEqual({ |
||
| 111 | 'Zaragoza': { |
||
| 112 | cityName: 'Zaragoza', |
||
| 113 | count: 2, |
||
| 114 | latLong: [ zaragozaLat, zaragozaLong ], |
||
| 115 | }, |
||
| 116 | 'New York': { |
||
| 117 | cityName: 'New York', |
||
| 118 | count: 1, |
||
| 119 | latLong: [ newYorkLat, newYorkLong ], |
||
| 120 | }, |
||
| 121 | }); |
||
| 122 | }); |
||
| 123 | }); |
||
| 124 | }); |
||
| 125 |